package com.example.demo.information.service.impl;

import com.example.demo.common.utils.Constant;
import com.example.demo.information.entity.Historyphoto;
import com.example.demo.information.entity.Photo;
import com.example.demo.information.entity.Student;
import com.example.demo.information.repository.HistoryphotoRepository;
import com.example.demo.information.service.HistoryPhotoService;
import com.example.demo.information.service.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class HistoryPhotoServiceImpl implements HistoryPhotoService {
    @Autowired
    private HistoryphotoRepository historyphotoRepository;

    @Override
    public String saveTempPhotoByStudent(Student student, String spId, String bkColor
            , String truePath, String resultSavePath, String internetPath, String xgr) {
        Date date = new Date();
        String xgsj = Constant.formatter.get().format(date);
        Historyphoto tempPhoto = new Historyphoto(student);
        tempPhoto.setLrr(xgr);
        tempPhoto.setLrsj(xgsj);
        tempPhoto.setYtpath(truePath);
        tempPhoto.setApipath(internetPath);
        tempPhoto.setSavepath(resultSavePath);
        tempPhoto.setSpid(spId);
        tempPhoto.setBkcolor(bkColor);
        historyphotoRepository.save(tempPhoto);
        return "success";
    }

    @Override
    public Historyphoto findByXsxxIdAndXxxxIdAndBjxxId(Long xsxxId, Long xxxxId, Long bjxxId) {
        List<Historyphoto> list = historyphotoRepository.findByXsxxIdAndXxxxIdAndBjxxIdOrderByLrsjDesc(xsxxId,xxxxId,bjxxId);
        return list!=null&&!list.isEmpty()?list.get(0):null;
    }

    @Override
    public Historyphoto findById(Long historyPhotoId) {
        return historyphotoRepository.findById(historyPhotoId).get();
    }


}
